﻿Imports System.IO
Imports System.Data.Linq
Imports TNCBAL
'Imports TNCDAL

Public Class ucBackup
    Public isBackup As Boolean = True

    Private objRecentBackupController As New RecentBackupController

    Private Sub ucBackup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoValidate = Windows.Forms.AutoValidate.Disable
        InitBackupInfo()
        BindGridView()
        FormatDataGridView()

    End Sub

    Public Sub FormatDataGridView()
        Dim renameColumns As New Dictionary(Of String, String)
        renameColumns.Add(gridViewRecentBackup.Columns(0).Name, "Backup By")
        renameColumns.Add(gridViewRecentBackup.Columns(2).Name, "Backup File")
        CViewer.FormatDataGridView(gridViewRecentBackup, Nothing, renameColumns)
    End Sub


    Private Sub BindGridView()
        Dim query = From tbl In objRecentBackupController.GetTop10RecentBackupList()
                            Select tbl.tblUser.UserName, tbl.CreatedDate, tbl.BackupFile

        Dim source As New BindingSource
        source.DataSource = query
        gridViewRecentBackup.DataSource = source
        BindingNavigator1.BindingSource = source
    End Sub

    Private Sub btnBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click
        If isBackup Then
            'mo form cho nguoi ta chon co so du lieu
            FolderBrowserDialog.SelectedPath = ""
            txtFolderUrl.Text = ""
            FolderBrowserDialog.ShowDialog()
            txtFolderUrl.Text = FolderBrowserDialog.SelectedPath()
        Else
            'mo form cho nguoi ta chon co so du lieu
            OpenFileDialog1.Filter = "Backup File(*.bak;*.dat)|*.bak;*.dat|All files (*.*)|*.*"
            FolderBrowserDialog.SelectedPath = ""
            txtFolderUrl.Text = ""
            OpenFileDialog1.ShowDialog()
            txtFolderUrl.Text = OpenFileDialog1.FileName()
        End If
    End Sub

    Public Sub InitBackupInfo()
        'chon thu muc cai dat SQL server
        txtFolderUrl.Text = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\TNC_DataBackup"
        'kiem tra xem duong dan nay co khong
        Dim thuMucSQL As DirectoryInfo = New DirectoryInfo(txtFolderUrl.Text)
        If (thuMucSQL.Exists = False) Then
            thuMucSQL.Create()
        End If
        txtBackupFile.Text = String.Format("TNCBackup_{0}-{1}-{2}.dat", Date.Now.Day.ToString(), Date.Now.Month.ToString(), Date.Now.Year.ToString())
        'txtBackupFile.Text = "TNCBackup_" + Date.Now.Day.ToString() + "-" + Date.Now.Month.ToString() + "-" + Date.Now.Year.ToString()
    End Sub

    Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
        Try
            If Me.ValidateChildren(ValidationConstraints.Enabled) Then
                Dim backupFile As String = txtFolderUrl.Text & "\" & txtBackupFile.Text
                Dim dbName As String = GeneralClassBAL.GetGiaTriThanhPhan("Initial Catalog")
                Dim objDBController As New DBController
                If objDBController.BackupDatabase(backupFile, dbName) Then
                    MessageBox.Show("Backup failed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    'Add recent backup
                    objRecentBackupController.Insert(backupFile)
                    'Load gridview
                    BindGridView()
                    MessageBox.Show("Backup succeed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim mypanel As DevComponents.DotNetBar.PanelDockContainer = Me.Parent
        'Dim mybar As DevComponents.DotNetBar.Bar = Me.Parent.Parent
        'mybar.Items.Remove(mybar.SelectedDockContainerItem)
    End Sub

    Private Sub txtFolderUrl_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFolderUrl.Validating
        Dim cancel As Boolean
        If String.IsNullOrEmpty(txtFolderUrl.Text) Then
            cancel = True
            Me.ErrorProvider1.SetError(txtFolderUrl, "Chưa chọn thư mục chứa dữ liệu!")
        End If

        If (txtFolderUrl.Text.Length > 255) Then
            cancel = True
            Me.ErrorProvider1.SetError(txtFolderUrl, "Tên thư mục không được quá 255 ký tự!")
        End If
        e.Cancel = cancel
    End Sub

    Private Sub txtBackupFile_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtBackupFile.Validating
        Dim cancel As Boolean

        If String.IsNullOrEmpty(txtBackupFile.Text) Then
            cancel = True
            Me.ErrorProvider1.SetError(txtBackupFile, "Tên cơ sở dữ liệu chưa có!")
        End If

        If (txtBackupFile.Text.Length > 50) Then
            cancel = True
            Me.ErrorProvider1.SetError(txtBackupFile, "Tên CSDL không được quá 50 ký tự!")
        End If
        e.Cancel = cancel
    End Sub
End Class
